Optim/free cell count

Also a change in the way we compute the weight, in order to take in account the matrix path (instead of just taking the end matrix).