Refactored and cleaned code