Made some major optimizations.

Lots of unnecessary sessions were being opened. This should fix that. Unfortunately it does still crash sometimes though.