Refactor and tidy