First attempt to solve infinite loop