Greatly improved flex logic