Clean up and fix some codes