Fix bugs, add "get_nth_item_index"

Added a "get_nth_item_index" function and fixed a few compile-time and
run-time errors.