logic clean-up and fixes