Add comments, fix a possbile threading issues