Incorporated changes by @DavidPS