Refactored and tested

Don't merge this in, just for viewing