adding tests, and refactorign for ease of testing."