Fixing memory errors and build problems