Refactored the code and added doc

Signed-off-by: benjamin <beness.j@gmail.com>