Adding Mike Heinz's code under GPLv3 with Mike's permission