Unit testing and bugfixes

Added jasmine unit tests (incomplete)
Various bug fixes as a result of unit tests