fixed minor bugs (indexing and file closing)