Move more variables to dynamic memory

This makes the code consistent and slightly simpler.