removed globals and some improvements