Refactor working code and spec files