Readability changes and bug fix from pull-request #2 (manually cherry-picked)