-fixing #2434, plus some code cleanup