refactored a bit and added jasmine tests.