Total overhaul of the code