Moved functions out of main.py to /lib