restructured the code, improved pipeline (especially error handling)