Code restructured

Made code nicer and optimized it to make it easier to implement batch
processing (e.g.: 'mydir','vtocp' and my 'image' are now all global
structures)