updated and reorganized brian/kori's code