Various fixes and restructuring.