Make algorithms more type-stable (fix #38)