Refactor python layer to be more modular