Finished the optimisation, haven't removed some of the debug stuff in case more errors come