Refactored and works