moved encoding to the top and added termencoding