clean up and refactor code