Fixing up tests and refactored code