Remove duplicate code.
Simplify error function.