Fix on-open and on-close to support chaining [fixes #81]