few bug fixes and a bit of cleanup