Improved error handling and program structure