Merged asolntsev code simplification fixes (with some small modifications).