Seems like there should be some higher-level API call that takes care of details like this.