fix check of headers and some refactoring