calculated the stack intersection handling lots of simple cases