Bit of refactoring and a bug fix