Refactored common code into utils.py