add exception handling for Function and some debugging precondition checks