Sorted lots of stuff and cleaned up code