Added support for invert param
Cleaned up stream cleanup