Rewrite codes and made changes