complete overhaul and simplification coming in next commit