Code refactor.

Some methods still need to be refactored.