Huge refactoring, discovered some critical flaws.