Reorg function

/me thinks this should work...

There are also a couple of bug fixes. :P