first working version without any segfaults