Refactor code, remove unused code