Some tidying up and bug fixes