major refactoring. Code is much cleaner