Improved Error Codes and Casting

Improved error code and replaced C style casts with their c++
equivalents.