re-wrote code for layerwise training