Added closeStream() method for graceful shutdowns