fix issue with detecting last row; also fix linewrapping