Add error calculation and integration tests
Fix memory leaks and out of bounds reads