Major refactoring. Now with more voodoo