Cleanup header/namespace pollution
Fix build with clang33, std::max is in <algorithm>
Use std::atomic for refcount
Fix more namespace pollution
Rename n to refs
Use nullptr