modified recursive procedure, added map