cleaned up sorting functions a bit. needs more work.