Refactor duplicate codes [issue#50].

Signed-off-by: Dhananjay Nakrani <dhananjaynakrani@gmail.com>