refactor and tidy