further tidying and bug fixes