Major code refactoring. Starting to feel somewhat stable.