Bringing in fixes from the edelmanchi version of this repo.