Tidied up comments, added optimisations and removed excess code