bug fixes, more tolerant to undefined