first version, slow and ugly