Initial round of cleanup and testing. Branching and minor elements remain to be tested.