some refactoring
write result into file