Move exit(0) to outside of if-loop