complete code overhaul, no functional changes