Update to a more efficient way and new algorithm