final refactored