added nil checks as discussed