major refactor and cleanup