Better error handling and removed a couple debug print commands