improved code according to scrutinizer