updated with new code to support many hierarchies