Correct memory swapping and preliminary threshold algo