Fixes #363 Invalid cast exception