Code refactoring and functionality adding