Fixed endless while loop