Another major refactor to clean up the code