Cleaning and correct potential bugs