refactor: better headers parsing and minor improvements