Very big refactoring. Things much easier to comprehend.