refactored javascript with riley's help