some logic changes,removed bugs