cleaned up the code and added support for emscripten