Prevent infinite while loop