Fixes error when line after id is not a string

Also moved function call to lastNonEmptyLine outside of the while loop so it's not called on every iteration.