Refactor build logic and fix more pylint warnings