Adding null check and fixing query on Controller example