Refactor and tidy up