converted $type into session. May be buggy.