added file close for destination files

It's a quick fix which closes the file after writing into it.
More code refactor needs to be done which will be taken up next.