more refactoring of algebra code