it'd be nice to wrap this in a method to make this overall algorithm more readable