Fixed #8, added onBeforeClose callback as suggested by @AnatolyRugalev