Total overhaul. Check commit for details