major refactoring and code simplification