new code and bugfixes to the first one