function is now working with pointer. fixed some bugs