Add some basic sanity checks and error handling