changes in logic. previous version is crap. does not work properly at the moment