Fixed Bugs and Added Some Error Handling