major refactor, added more tests