Added some type checking before casting. Closes #5

Signed-off-by: Markus W. Mahlberg <markus.mahlberg@me.com>