Added some new functions to the runtime and refactored code.