Improved one error check, renamed key variables.
Added set sorting functionality.