I don't like this duplicate code, but the type system seems to leave me no choice here. @anlu any ideas about how I can keep this logic all in one place?