Fix parser bugs and improve diagnostic messages