backport pull requests #96 and #97 to 1.9 branch

(cherry picked from commit 30990f14f21621ee9a7c9bdb47af63bdcb23b59a)