Re-organised code to a more reasonable structure.