Restructured main flow, now checks permissions