refactored and added tests and spec helpers