finally refactored