working version but taking more than 5 min