Cleanup, improved checks, and optimisations