Final working code

Retroactively adding final, working version of code