Added functions and cleaned code