a bit of optimisations and code cleaning