reduced duplicate code as per @eddiemonge suggestion