Correctly checking for infinite loop

Not much else has bee integrated. Testing
still needs to be performed.