Layer Formation in Stably and Unstably Stratified Flows