Refactored and fixed.