clean and fix bad logic