Fixes bug 551
Merge pull request #562
merge #562

Thanks @mtraynham!
cut duplicate of function

fixed #571