Refactored version