fixes various invalid read errors
fixes invalid read

Valgrind reported an invalid read at this point. Googling suggests
this might be a valgrind error. Still, casting solves the issue.