added volatile keyword to prevent optimization related bugs