adding first version of ric's faster algo