Algebraic Thomas decomposition