improved algorithm, added Refs output in STUTableOutput