Initial working version: slow and slightly different from the Fortran version.