Rewrite in python to allow complex logic.