rebased and fixed small bugs