This code is being called in two different places I think looking at the surrounding if/else this could be refactored to have the else case processed first and then this code executed once and last if required.