Cleaning code and multithread support