fix many bugs & clean up