clean up and some logic fixed