Simplify code and use multithreading