This looks like a  duplicate code block with L20 - L32, could we reduce this with a common function call?