refactr to prevent infinite loop on ios5