Improved error handling in main