Move some of the code in from the command-line tool to contacts-cache, for possible re-use. Clean up a bit while doing so.