Fixed file overwriting bug