fix for issue 391 and issue 307
Merge pull request #407 from qfan/master

fix for issue 391 and issue 307