refactor and fixing tests