Code cleanup and refactor for more modularity