refactored version