Fix for issue #671 and implementation

As per the comment on the reported issue #671, sending the pull request.