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>