added many optimizations to intersection code