adaptating segment algo first working