refactoring bad code