integrating fixes and tweaks from an old branch i was working on