Correct error handling and catch SIGINT for cleaner exit