Fixed infinite loop that was occuring due to an incorrectly refactored function