Added OutOfMemoryError checks. If it catches it, clears the GC then tries again.