Fixed bugs with functions