refactored, fixed pivot bugs