Added a safety check and fixed a couple of warnings.