Cleaned up warnings, and added checks to malloc return values and error log writes in case of failures.