refactor some things to reduce allocations