Re-use newPivotStatement function

Signed-off-by: Dries Vints <dries.vints@gmail.com>
Various fixes

Fixed typos, incorrect property and incorrect return types.

Signed-off-by: Dries Vints <dries.vints@gmail.com>
Merge branch '4.0'