Remove unused code and refactor