algorithm works now