Refactored code.  Added sorting and modified extract fields.