Some more refactoring, mostly in engine.js