Updated code to match Pomax's suggestion.