Updated Main loop