convert to class and add some checks