major refactoring to improve performance
cleanup