Added code for functions - untested

First commit with new code added. Not yet tested.