Modifications for memory efficiency + no use of masks anymore.