fixed bugs with functions and add few tests