Commented/refactored/modularized all code