Reuse some code
Replace vector with map to reduce memory usage
More testing