added type checking, fixed to_dict() function