@redconfetti can we put this in a separate function and add some documentation on what it's doing?