Performance improvements in main loop