starting to refactor, might have caught bug too