fix insidious logic & arithmetics bug