Added main loop with exit condition