Added comments and corrected code