add error handling to functions.py
add error handling to functions.py