added some synchronized statements to try to help with threading