Modified serial code to use thread