Revision to 51fb065f courtesy of developer Steve Matthews. Slightly more efficient.