bugfix and change structure of code