Buggy implementation of find_index due to my lack of understanding