updated algorithm, better accuracy