This logic is duplicated a few times.  I think you can factor this out into a func