refactor leader finding algorithm and unit test it