Refactor and cleanup in multithreading examples