added more error handling, combined lines, remove tmp contents