Added logic to handle files with different number of lines