Refactoring code to be simpler