More refraction work. Having lots of problems with segfault at random intervals