fix bug in main program and add lock to read vs. clean