Initial refactor with logging, use of constants, and some minor clean-up.