Memory

Fix various memory problems and segfaults