Tidy up and corrected minor bugs