Some more bug fixes and refinements