Dmitriy's solution