corrected a few bug found by Andrey Zamkovenko (https://plus.google.com/+AndreyZamkovenko)