Added code to main loop