integrated some changes from GH# 249