fix #822 & related mess

tested a little, looks ok to me.