refactored and cleaned up some code.