code refactoring in utils.py