Refactored code - still untested