write templates on close event instead of exit