Cleaned up bugs and tidied up error checking code. auto_poly_generator not finished.