build as C++
move static functions into anonyomous namespace and whitespace
Use RAII to clean up handles