Rewrite validate to handle validation in both directions, and clean up output.