almost total refactor