Added error handling and other core JS functions

Not 100% complete, but the basics are there. Left some TODOs in the
code.