v3.0: removed global vars. More robust approach; using a struct