Final version for serial AD

No known bug detected. From here on, parallel AD will be implemented.