Fixing bug in new function