algo seems OK, but not so efficient finally