Moved over some loose ends from reference implementation.