refactor
prevent excessive memery use