Minor changes in design, fixed bug with volatile variable