Move the majority of code from tool.go to a lib in order to allow other go programs to use