Extract loop handler to separate file