Refactor: fixed dangerous casts

Pair programming done