Refactorization of top funxctions and minor functions added