Refactor utils.py