Finished factoring out clean and directed functions with tests