Refactoring in preparation to fix a bug