refactor almost complete