Better error messages and minor cleanups