shortened the code a little bit more, and added error messaging when the type is mistaken