Begun refactoring so testable and fixes issue #71