Refactoring to make code dryer and less complex