Formatting changes added.
Extracted repeating code segment to a method.