Add setup.py
tidy up sorting functions