Added comments and cleaned the code a bit