refactoring and full RFC 6637 support