Fix for main algorithm
Redesign for more security