Refactor most of code