added some basic ordering

and rearranged the code a bit