move buffer and file related code out of thread.c