Modified python implementation.