Add error checking here. C++ does one thing right - overloading those library functions so I won't need these stupid casts.
Update callers.