move kernel test outside the main loop