refactoring - using closure to get rid of repetitions