Half way through writing pivot flatten function.