really complex refactoring, i probably overcomplicated things