cleanup and refactor code