Call get_layer instead of layers->[] (more efficient)

```layers``` now recreates the array, so this made looping over layers with ```layers->[$i]``` be O(n^2)

The semantics of get_layer are slightly different though, because it a. doesn't support negative indices, and b. throws a C++ exception if the index is invalid, instead of returning undef.