Fix a bug in callFunction method
Error handling and tests.