Adding validation, and better error handling