Error handling and prevention added

added a bunch of checks to stop error from happening