fix bug; simplify code