Parse errors correctly

Fixes #1072
Needs tests