Re-arranged a bunch of stuff. Needs testing