Minor refactoring

Also identified a bug