More cleanup and refactoring. Write and read left for tcp