function now working, though errors aren't dealt with yet