Added all code changes suggested by Vladislav