General refactoring of code and increased test coverage Reviewed-by: lancea, aefimov
Reviewed-by: psandoz