Code generalized from the opinion model project