fixed and refactored