Cleaned up dereferencing memory -- moved it all to a separate method