function almost compelete