Fixed casting issues. Improved an error message.