fixing bugs in engine.js