various optimizations and code cleanup
various optimisations