Some cleanup of growable arrays:

* moved declarations closer to where they are used
* assertions
* documentation
* added const where possible
* some early returns to avoid doing unecessary work