improved robustness to deal with different loop bounds, DOS line endings, and fixed nesting bugs