Quite a lot of simplification based on using ComPtr.