Merging code from the PR of @jj0hns0n. Thanks Jeff!