Fixed indents.
Extracted some common logic to util.py.