Refactor and begin adding tests