Finally sort out memory issues (hopefully)