refactor update-weights function so that matrix structure in weights is preserved