added function to stop recursion. this is the best one.