Refactored and working