Add global index cleanup function.
Fix location of sem_wait().
More comments.