Some tidying and a bug fix