Refactor and cover by tests

@adelton could you please review or just merge and let @ekohl review?