replace panic with error return value