Simplified code by refactoring