Improved aligntable algorithm