fixed bugs with functions and added few tests