simplify error checking code a bit