clean up code, general optimisations