python rewrite with better validation/isolation