Updated, tested and merged pzinovkin's patch.