added memory checks