Revert "Revert "Revert "some code refactoring"""

This reverts commit 31ee0d777b69a47d799f2eb33c0add32e277a38a.