issue #864 and some minor fixes