Refactored some things to enable us to reuse code.