use spawn instead of fork. extract listeners to function.