Added some tests and bit of refactor