Important code related fixes by Pavel Pimenov