fixing issue #9 where end event being called before finish writting to file