refactored pivot algorithm out of main code