took a stab at refactoring but it wnet nowhere