final version, tested by Demidchuk