Add docstring; move function declarations outside try block