Refactored and fixed 'infinite loop'