serious refactoring, the code will be clearer on this than any comment owuld be