Added comments for all functions and cleaned the code a little