Cleaning the code adnd handling errors