Code cleaner now. Added functions and comments