Added matjags function