Cleaned up parts of code that were unacceptable in terms of safety or execution speed - such as changing argument copies to references, adding checks for null parameters at start of routines, and fixed various memory leaks. While much of the code is still very messy, it should now at least run faster and safer. Regression testing passed, showing that protective methods are still working fine.