adjusted javascript function based off of example app