Trying to fix infinite loop