tweaks and final consolidation of code from ages ago