small formatting changes, better error messages and checking