refactoring and simplifying code