A new deterministic algorithm for factoring polynomials over finite fields is presented. This algorithm makes use of linear algebra methods and is an improvement of the Berlekamp algorithm, as well as that of Niederreiter, in the case of nontrivial algebraic extensions. The improvement is achieved by a new method of computing a basis of the so-called Berlekamp primitive subalgebra that makes use of an idea related to the field of Gröbner bases. Finally, some comparative running times show how this new deterministic algorithm performs better than other probabilistic algorithms in some practical cases.