Change by Matthew Wakeling: changed procedures to the new calling conventions which improves performance.