Added some tests and made code slightly more testable