added counter in file writing