Refactored to use common variables consistently, even though it's a hideously verbose way of tackling it