Finally resolved all compile problems and all tests pass! The code is pretty ugly, but it works.