fixed cast order of operations