Cleanup various algorithms and try to match spec style

Nothing major here.