Rewrote script from scratch