Fixed bug in plot loop