Code cleanup + minor optimisations