improved code + unit tests