Fix wait loop at end of program.
Remove debug code, add a little bit more informational output because optimizing the database takes forever.