Replace panic with error return