add support for custom function calls and refactor