Replaced loops with single main loop after page load