Extracted on-last-line check to function and added a couple of comments