cleaned version but exceed time limit