Massive refactoring and adding some polish