Safe casting functions

Oh, right. Still...