add content-length and some refactoring