Improved previous logic