Additions to patch supplied by Tim Wilkes

Final Credit for the pull request to me

Rebased for 0.20.7 and updated coding style on if statements