Added better checking for nil graphics contexts.