added nil check and missing return