add mode parameter; remove duplication in the command-line case.