Code cleaning and minor fixes