Refactored the main exec function into a multipass operation and fixed a bug when calling functions