Refactored to suit my tastes