refactored and cleaned up code. final commit.