Major refactoring. Still need more.