Tidying up and bug fixes