Refactored very slightly