Fixed unchecked/unsafe casting and stuff

Signed-off-by: Justin Wiblin <Justin_wiblin@hotmail.co.uk>