This is the same patch provided by PR #550, but rebased against the current develop branch. It also passes the flake8 checks.