Removed some printf(); fixed error handling in while loops