Rewrote to be cleaner (from pull-request comments)