Smoother and cleaner code. Also adapted a few things