A few performance tweaks to reduce the amount of looping needed to build the table