refactored an unnecessary if-else loop