Optimised for speed, fixed allocation bottlenecks, added more code comments