updated controller to handle undefined query results