This function needs better test coverage.