Added check to prevent a potential infinite looping scenario