Refactored, added more tests