Modified error handling, other small fixes
Use temp dir rather than saving file locally