Working version of jacobi algorithm