Fixed quit loop and improved performance of exit loop.