writing function for depth search