[DF-5253] Memory usage optimised. Code refactor to make it easier.