some code cleaning and bug fixing