Small code updates preventing notices with incorrect input

I have been browsing through the code and I'm suggesting several small enhancements for protecting against incorrect input data.

Feel free to reject/comment on these.