Improved error handling and commented code