Improve error handling and fix infinite parse case