Improved error messages and moved safety check
Fixed build error