Here I think you need to use floor(), don't you?  If the thumbnail is the last one in the row, you round up to the next row and then it won't be shown, right?