improve error handling, fix some parsing bugs