Refactored the code to make things a bit cleaner and easier for testing.
Merge remote-tracking branch 'origin/lee-dev' into lee-dev