Tuning Magnetism And Superconductivity In Layered Chalcogenides And Pnictides