Properly shut down program when the stream is finished.
Start moving things in the context object.

Also, use "smart" pointers with Ptr in order to prevent a tricky
double free in feature detection.
Factor together some other repeated code.