Added comments and fixed small bugs