Replaces logic to work around bug in previous commit.