code to replace the pull -request 581 and issue #138, thanks to @randomecho for the code