Re-write of previous (hasty) fix.
This is cleaner.