First working version of refactored code.