refactor, add tests

Command line tool is broken right now. Up next is fixing it.