Move result functions outside main loop