Major code tidyup; organised into packages