Error plotting and code reorganization