Implementation copied from internal project