added improved algo