Refactored and added new tests and cleaned up some code