better type checking, could improve even more