Rewrote a bunch of stuff to get rid of the big switch and if blocks. Removed all explicit allocation. Using std::vector instead of arrays to transport keys around.