fix logic bugs