Refactor and test! Hooray.