Improved error handling in the main function (thanks Darkz!).