Reduce duplication. Also improve error message