Bug fix and tidy tests