optimize master and worker code