Final commit. Added more tests and modified some logic.