refactor update-weights function