How about moving this function definition out of while loop?