Replace system calls with Python functions; update tests