Cleaned up functions.py and reworked flow, taking the function declaration out of the loop.