Finish removing C style typecasts, NULL, and add more error checking