Fixes #1488. Also implements close callback