Use pimpl idiom to encapsulate AD implemntation (#397)