completely reworked to save the memory