tidying up, optimisations, commenting