some slight refactoring I did while reading the source