Fix bug and tidy