Fixed up code to make it work

Thanks guys at Stack Overflow!