update to work with non-square costs