remove copypaste code by extracting function