Updated code to be modular and more efficient. Cleaned up and reformatted.