fixed logic error, confirmed some algo logic