Switched order of conditionals for robustness and changed to using naturalHeight for images
Added optional caching diagnostic messages