improvements suggested by Tom Barron on codereview.stackexchange.com