Add some more sanity checks and better error messages