lots of cleanup and tweaking