Recfactored some of code