Refactoring a bit to work on 1.8.7