fixed memory errors on large files. simplified logic.