refactored and cleaned code