Improved internal error handling and added new test cases.