refactored and cleaned further