Improves error handling

Better error handling and instance checking. some functionality is abstracted.