implemented ordering

style has to be improved.

also functions are still kinda ugly.