refactory code from tasoha.com