lots of cleanup and reorganization