Completely rewritten with a more gulp-friendly logic.