Lots of refactoring to support gcc 4.8.2 and clang 3.4.

sandbox compiles so far, tests and benchmarks still need more work.