Rewrite a016 code, original version is very ugly.