Correcting array index error and updating file handler