Moved duplicated code from the command line scripts to a library.