Checks for errors before inserting data. Small fixes and optimizations.