Return error codes on error. Added check for missing fields.