hw4 huge refactoring