Could use a bit of instanceof removing cleanup, but not critical