Moving code around for tabular legend