lot of code changes and critical bugfix