Move some functions to util.py