a lot of refactoring to get array access working