this code segfaults, but i don't know why