Implementation of F4 algorithm