Restoring code

Restoring code written from 3rd to 8th of Oct, which was deleted from master branch by Ilya's commit. Also contains recommit of Ilya's a88f102ea802759db2e56570de537190c00c55be commit.