Code tidying

Split into smaller modules, easier to maintain.