Fixed wrong behaviour of encoding function