some refactoring and banging my head against a brick wall