adds memory checks, needs to be refined