refactored and commented some more