First version (does not preserve input order)