almost finish the function