Alpha implementation of clustering
Reorganized code and added several utility functions to help with tree traversal.