Added initial qunit tests and did some refactoring to make testable