cleanup and refactor