Refactored slightly