Huge number of changes pertaining to Reference implementation. Some code cleanup as well.