Minor refactoring of lib and adding tests