Refactoring
Further changes, seem to have fixed the segfault upon quit (without any idea how)