Major refactoring. Still working on niceties.