fixed early exit in algo and bugs