Refactored the plugin to make the exit logic more sensible and independent from nested if statements.