finally fixed all the functions