Clean up the code and corrected errors