changed typeof check to instanceof