Refine python wrapper
Avoid unnecessary memory use