Moved test out of main loop