Lots of refactoring. Much cleaner code