first version, very slow and poorly written