Implemented minimizeError() function