changed double quotes to single, added typeof undefined checks throughout