improve mode handler