Lots of bugfixes and performance optimizations. Could still use a lot of perf optimization.