slight refactoring, bug fix