changed break to exit so that all resources are freed