Small detail: you could move the `cols` defenition within the if