Cleanup git tree, fix bugs, add unit tests