Minor fixes and refactoring of functions and spec