Fixed memory leaks
Changed the way function calls are handled