And the last bits that need fixing; the code is still less efficient than desired but should now not crash.