I've refactored this to make it clearer