A few small suggestions.

While not incorrect, sometimes it helps with readability to use temporary variables for for loops that are only used in one place (i.e. the functions).