JS optimisations and cleanup