Globally change all index variables to ssize_t

I hope I did it correctly for all functions. I also optimized and fixed some code apart from the ssize_t changes.