refactor, using base code from online example