Some code overhaul