Various edits improving performance, closing memory leaks and fixing bugs causing crashes