updated code that had been untested from notepad