Minor cleanups
Optimisations guided by profiling

This results in a 50% speedup of the code.