fixed and cleaned code