Small refactor. Added more tests as well.