Added comments, cleaned the code