Related to #43, #53, #51
I've combined some fixes for the issues above into a single branch. This MR is mostly to share the changes for testing and further development. Don't review the changes until we've had a discussion/test of the science results (and memory usage).