major refactor in accordance with best practices