New algorithm may make lithium-ion batteries safer