This is the code for tool.  I am still in the process of making proper comments.  Thanks for your patience!