Code refactoring

Code refactoring without syntax checking.
Need to check it work.