Finished the PG mainloop implementation.