Heavily cleaned up code to run more efficinetly and be more readable