maybe extract an auxiliary function for this ? it sound like you're repeating this a bit already and there's potentially for more.