Merged recent code reorg to make it work with current architecture.