code cleaning and small bug fix