Refactor code, reduce memory