refactor code and remove unused code